package org.mms.service.impl;

import org.mms.exception.BusinessException;
import org.mms.pojo.dto.MaterialTypeDto;
import org.mms.pojo.entity.MaterialType;
import org.mms.pojo.request.MaterialTypeRequest;
import org.mms.repository.MaterialTypeRepository;
import org.mms.service.MaterialTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MaterialTypeServiceImpl implements MaterialTypeService {
    @Autowired
    private MaterialTypeRepository materialTypeRepository;

    @Override
    public List<MaterialType> list() {
        return materialTypeRepository.findAll();
    }

    @Override
    public MaterialType getById(Long id) {
         return materialTypeRepository.findById(id)
                .orElseThrow(() -> new BusinessException("物资类型不存在"));
    }

    @Override
    public MaterialType create(MaterialTypeRequest request) {
        MaterialType entity = new MaterialType();
        BeanUtils.copyProperties(request, entity);
        return materialTypeRepository.save(entity);
    }

    @Override
    public MaterialType update(MaterialTypeRequest request) {
        MaterialType entity = materialTypeRepository.findById(request.getId())
                .orElseThrow(() -> new BusinessException("物资类型不存在"));
        BeanUtils.copyProperties(request, entity);
        return materialTypeRepository.save(entity);
    }

    @Override
    public void delete(Long id) {
        materialTypeRepository.deleteById(id);
    }
}